package ch07_GUI编程;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class EventListenerWithAdapter extends MouseAdapter {
	JLabel canvas = new JLabel();
	Graphics g = null;

	public static void main(String[] args) {
		new EventListenerWithAdapter().init();
	}

	void init() {
		BaseFrame f = new BaseFrame("继承默认适配器类监听事件");
		f.setLayout(new BorderLayout());
		canvas.addMouseListener(this);
		canvas.addMouseMotionListener(this);
		canvas.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
		f.add(new JLabel("用鼠标在下面写字"),BorderLayout.NORTH);
		f.add(canvas,BorderLayout.CENTER);
		//f.setSize(420, 140);
		f.setSize(600, 400);
		f.setResizable(false);
		f.showMe();
		
		g=canvas.getGraphics();
		g.setColor(Color.RED);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		g.fillOval(e.getX(), e.getY(), 3, 3);
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		//g.fillOval(e.getX(), e.getY(), 3, 3);
		g.drawRect(e.getX(), e.getY(), 10, 5);
	}

}
  